<template>
  <el-dialog
    title="请分配工程师"
    :visible.sync="dialogVisible"
    width="30%"
  >
    <SelectSearchId v-model="engineerId" label="选择工程师" :options="engineerList" labelKey="name" valueKey="name"></SelectSearchId>
    <SelectSearchId v-model="valueTime"  @change="changeTime" label="预计上门时间" :options="handleTimeList" labelKey="name" valueKey="name"></SelectSearchId>

    <span slot="footer" class="dialog-footer">
      <el-button type="success" @click="save">保 存</el-button>
      <el-button type="primary" @click="dialogVisible = false">取消</el-button>
    </span>
  </el-dialog>
</template>

<script>
import SelectSearchId from '@/components/SearchBar/SelectSearchId'
import * as OrderAction from '@/api/order'
export default {
  name: "assignDialog",
  components: {
    SelectSearchId
  },
  data () {
    return {
      dialogVisible: false, // 弹窗是否显示
      engineerList: [], // 工程师列表
      engineerId: null, // 工程师ID
      ordId: null, // 订单Id
      valueTime: null,
      handleTime: null, // 预计上门时间
      handleTimeList: [
        {id: 2, name: '预计2小时'},
        {id: 4, name: '预计4小时'},
      ]
    }
  },
  methods: {
    // 弹窗显示
    async show (ordId,list) {
      this.dialogVisible = true;
      this.engineerList = list;
      // console.log('工程师列表', this.engineerList);
      this.engineerId = null;
      this.ordId = ordId;
    },
    // 时间选择
    changeTime() {
      // console.log('选择的时间', this.valueTime);
      if (this.valueTime) {
        this.setTimes(this.valueTime)
      }
    },

    // 时间处理
    setTimes(value) {
      let timestamp = new Date().getTime();
      timestamp = timestamp + value * 60 * 60 * 1000;
      let dataAfter = new Date(timestamp);
      let month = dataAfter.getMonth() + 1;
      let day = dataAfter.getDate();
      let min = dataAfter.getMinutes();
      if (min < 10) {
        min = "0" + min;
      }
      if (month < 10) {
        month = "0" + month;
      }
      if (day < 10) {
        day = "0" + day;
      }
      this.handleTime =
        dataAfter.getFullYear() +
        "-" +
        month +
        "-" +
        day +
        " " +
        dataAfter.getHours() +
        ":" +
        min +
        ":" +
        dataAfter.getSeconds();
    },
    // 保存
    async save() {
      if (!this.engineerId) {
        return this.$message.error('请先选择工程师！')
      }
      if (!this.handleTime) {
        return this.$message.error('请选择上门服务时间！')
      }
      let param = {
        ordId: this.ordId,
        userId: this.engineerId,
        handleTime: this.handleTime, // 预计上门时间
      }
      const res = await OrderAction.orderAssign(param);
      if (res) {
        this.$message.success('指派成功');
        this.dialogVisible = false;
        this.engineerId = null;
        this.$emit('reload')
      }
    }
  }
}
</script>
